//
//  NetworkClient+User.swift
//  SitPosition
//
//  Created by apple on 2019/1/9.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import Alamofire


/**
 * 用户相关的网络接口
 */
extension NetworkApi {
    
    // 手机号码登录
    public static func logon() -> NetworkApi {
        return NetworkApi("/userCenter/users/action/login", method: .patch)
    }
    
    // 查询用户信息
    public static func queryUser(_ placeholder: Placeholder) -> NetworkApi {
        return NetworkApi("/userCenter/users/{id}", method: .get)
    }
    
    // 修改用户信息
    public static func udpateUser(_ placeholder: Placeholder) -> NetworkApi {
        return NetworkApi("/userCenter/users/{id}", method: .patch)
    }
    
    // 找回密码
    public static func resetPassword(_ placeholder: Placeholder) -> NetworkApi {
        return NetworkApi("/userCenter/users/action/resetPassword?code={code}",
                          method: .patch,
                          placeholder: placeholder)
    }
    
    // 获取手机验证码
    public static func fetchPhoneCode() -> NetworkApi {
        return NetworkApi("/authentication/codes/action/fetchByMobile",
                          method: .get)
    }
    
    // 手机号码注册
    public static func register(_ placeholder: Placeholder) -> NetworkApi {
        return NetworkApi("/userCenter/users/action/register?code={code}",
                          method: .post,
                          placeholder: placeholder)
    }
    
    // 客户端登出
    public static func logout() -> NetworkApi {
        return NetworkApi("/oauth/token", method: .delete)
    }
    
    // guest获取access_token
    public static func grantAccessToken() -> NetworkApi {
        return NetworkApi("/oauth/token", method: .post)
    }
    
    // 客户端刷新access_token
    public static func refreshAccessToken() -> NetworkApi {
        return NetworkApi("/oauth/token", method: .post)
    }
    
    // 获取环信token
    public static func fetchRingIMToken() -> NetworkApi {
        return NetworkApi("/authentication/im_token", method: .get)
    }
    
    // 查看用户收益
    public static func viewUserIncoming() -> NetworkApi {
        return NetworkApi("/userCenter/userIncomes", method: .get)
    }
    
}


/**
 * 用户相关的网络请求
 */
extension NetworkClient {
    
    // 获取Access Token
    func requestGuestAccessToken(_ grantType: String = "client_credentials",
                                 success: DataParamCallback?,
                                 failure: ErrorCallback?) {
        let params: Parameters = [
            "grant_type" : grantType
        ]
        let headers: HTTPHeaders = [
            "Authorization" : NetworkClient.clientInfo
        ]
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        request(NetworkApi.grantAccessToken(), params: params, headers: headers).responseData(completionHandler: completionHandler)
    }
    
    // 获取验证码
    func fetchVerifyCode(mobile: String,
                         type: String,
                         success: DataParamCallback?,
                         failure: ErrorCallback?) {
        // 获取验证码
        let params: [String : String] = [
            "mobile": mobile,
            "type": type
        ]
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.fetchPhoneCode(), params: params, headers: nil).responseData(completionHandler: completionHandler)
    }
    
    // 登录
    func login(mobile: String,
               password: String,
               success: DataParamCallback?,
               failure: ErrorCallback?) {
        let params: Parameters = [
            "mobile": mobile,
            "password": password.md5()
        ]
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.logon(), params: params)
            .responseData(completionHandler: completionHandler)
    }
    
    // 注册
    func registerUser(mobile: String, code: String,
                      password: String, username: String,
                      invitCode: String?,
                      success: DataParamCallback?,
                      failure: ErrorCallback?) {
        let placeholder: Placeholder = [
            "code": code
        ]
        var params: Parameters = [
            "mobile": mobile,
            "password": password.md5(),
            "account": username
        ]
        if let invitCode_ = invitCode, !invitCode_.isEmpty {
            params["recommendCode"] = invitCode_
        }
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.register(placeholder), params: params, headers: nil)
            .responseData(completionHandler: completionHandler)
    }
    
    // 找回密码
    func resetPassword(mobile: String,
                       code: String,
                       password: String,
                       success: DataParamCallback?,
                       failure: ErrorCallback?) {
        let placeholder: Placeholder = [
            "code": code
        ]
        let params: Parameters = [
            "mobile": mobile,
            "password": password.md5(),
        ]
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.resetPassword(placeholder), params: params, headers: nil)
            .responseData(completionHandler: completionHandler)
    }
    
    // 获取环信token
    func fetchRingIMToken(success: DataParamCallback?,
                          failure: ErrorCallback?) {
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.fetchRingIMToken(), params: nil, headers: nil).responseData(completionHandler: completionHandler)
    }
    
    // 查看收益
    func viewUserIncoming(page: Int, pageSize: Int = 15,
        success: DataParamCallback?,
        failure: ErrorCallback?) {
        let params: Parameters = [
            "pageNum" : page,
            "pageSize" : pageSize
        ]
        let completionHandler = commonDataRequestCompletion(success: success, failure: failure)
        NetworkClient.instance.request(NetworkApi.viewUserIncoming(), params: params, headers: nil).responseData(completionHandler: completionHandler)
    }
    
}
